<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *设备绑定
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$tabs=array(
	array('op'=>'list','title'=>'设备绑定列表'),
);
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(!empty($_RQ['apitype'])){
		$params['apitype like']='%'.trim($_RQ['apitype']).'%';
		$return['params']['apitype']=$_RQ['apitype'];
	}
	if(!empty($_RQ['id'])){
		$params['id']=intval($_RQ['id']);
		$return['params']['id']=$_RQ['id'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_aiagent_goodslog',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['member']=cfc('core')->member_show($item['mid']);
			$item['agenttitle']=pdo_getcolumn('jy_aiagent_useragent',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['agentid']),'title');
			$item['goodstitle']=pdo_getcolumn('jy_aiagent_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['goodsid']),'title');
			$item['goodslogo']=pdo_getcolumn('jy_aiagent_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['goodsid']),'image');
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$tips='这里删除绑定，API接口那边也会同步删除绑定的';
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','tip'=>$tips,'edit'=>false,'list'=>array(
			array('name'=>'title','title'=>'设备名称','type'=>'string'),
			array('name'=>'logo','title'=>'LOGO','type'=>'image'),
			array('name'=>'member','title'=>'客户信息','type'=>'member'),
			array('name'=>'code','title'=>'配对码','type'=>'string'),
			array('name'=>'macsn','title'=>'MAC','type'=>'string'),
			array('name'=>'deviceid','title'=>'硬件ID','type'=>'string'),
			array('name'=>'apitype','title'=>'接口参数值','type'=>'string'),
			array('name'=>'agenttitle','title'=>'绑定智能体','type'=>'string'),
			//array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
			array('name'=>'createtime','title'=>'时间','type'=>'time'),
		)),
		'search'=>array(
			array('name'=>'title','width'=>15,'title'=>'设备绑定标题','type'=>'string'),
			array('name'=>'apitype','width'=>15,'title'=>'接口参数值','type'=>'string'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择设备绑定！','error');
	}
	$post=get_postdata();
	if(isset($post['status'])){
		pdo_update('jy_aiagent_goodslog',array('status'=>intval($post['status'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			$goodslog=pdo_get('jy_aiagent_goodslog',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			$this->model('api')->delusergoods($goodslog,1);
			pdo_delete('jy_aiagent_goodslog',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}